﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;

namespace balace_project_new
{
    public partial class Form1 : Form
    {
       
        #region global variables
        int control = 1;
        int c = 1;
        USER[] structUser = new USER[4];
        // define "admin table" variable to contain data of user/pass loaded from database
        balance_schemaDataSet.adminDataTable ad = new balance_schemaDataSet.adminDataTable();
        balance_schemaDataSetTableAdapters.adminTableAdapter adAdapter = new balance_schemaDataSetTableAdapters.adminTableAdapter();
        #endregion

        #region form_load
        public Form1()
        {
            InitializeComponent();
        }
        struct USER
        {
            public string user;
            public string pass;
            public int numberOfGateway;
            public int IpIndex;
            public int use;
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            // load user/pass from database to ad Table and parse it to structData.
            // We use structData to verify user's informations.
           adAdapter.Fill(ad);         
           for (int j = 0; j < 4; j++)
           {
               structUser[j].user = ad.Rows[j]["User"].ToString();
               structUser[j].pass = ad.Rows[j]["Password"].ToString();
               int.TryParse(ad.Rows[j]["IpIndex"].ToString(), out structUser[j].IpIndex);
               int.TryParse(ad.Rows[j]["NoOfGateway"].ToString(), out structUser[j].numberOfGateway);
               ad.Rows[j]["Use"] = 0;
               structUser[j].use = 0;
           }
        }
        #endregion

        #region check user

        private void button1_Click(object sender, EventArgs e)
        {
            apply();
        } 
       private void apply()
        {
            string s, s1;
           // compare user/pass bewteen user and database
           // if true, form2 will be loaded, the presence of the user who logins will be update to database
           // Otherwise, Error dialogue will be shown.
            s = txtUser.Text + txtPass.Text;
            for (int j = 0; j < 4; j++)
            {
                s1 = structUser[j].user + structUser[j].pass;
                if (s == s1)
                {
                    structUser[j].use = 1;
                    ad.Rows[j]["Use"] = 1;
                    adAdapter.Update(ad);
                    Thread r = new Thread(new ThreadStart(run));
                    r.Start();
                    Close();
                    break;
                }
                else
                {
                    if (j < 3) continue;
                    else
                    {
                        control = 0;
                        txtPass.Clear();
                        txtUser.Clear();
                        var m = MessageBox.Show("                   CẢNH BÁO  \r\r Sai tên đăng nhập hoặc mật khẩu \r\n         Vui lòng đăng nhập lại !", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
           
        }
       private void run()
       {
           Application.Run(new Form2());
       }
       #endregion

        #region close form
       private void button2_Click(object sender, EventArgs e)
       {
           Close();
       }
       #endregion

        #region when user press ENTER, the codes below will be excuted, it calls apply() function
       private void txtUser_KeyUp(object sender, KeyEventArgs e)
       {
           if ((c == 1) && (e.KeyCode == Keys.Enter))
           {
               apply();
               if (control == 0) c = 0;
               timer1.Enabled = true;
           }
       }

       private void txtPass_KeyUp(object sender, KeyEventArgs e)
       {
           if ((c == 1) && (e.KeyCode == Keys.Enter))
           {
               apply();
               if (control == 0) c = 0;
               timer1.Enabled = true;
           }
       }

       private void timer1_Tick(object sender, EventArgs e)
       {
           c = 1;
           timer1.Enabled = false;
       }
       #endregion

        #region color for buttons
       private void button1_Enter(object sender, EventArgs e)
       {
           button1.ForeColor = Color.Blue;
       }

       private void button2_Enter(object sender, EventArgs e)
       {
           button2.ForeColor = Color.Blue;
       }

       private void button1_Leave(object sender, EventArgs e)
       {
           button1.ForeColor = Color.Black;
       }

       private void button2_Leave(object sender, EventArgs e)
       {
           button2.ForeColor = Color.Black;
       }
       #endregion

    }
}
